import sortedcontainers


class Solution(object):
    def brightestPosition(self, lights):
        d = sortedcontainers.SortedDict()
        for p, r in lights:
            d[p - r] = d.setdefault(p - r, 0) + 1
            d[p + r + 1] = d.setdefault(p + r + 1, 0) - 1
        v = list(d.values())
        max_v = v[0]
        max_val = 0
        for i in range(1, len(v)):
            v[i] += v[i - 1]
            if v[i] > max_v:
                max_v = v[i]
                max_val = i
        return d.popitem(max_val)[0]



